/**
*      author: Y_Yao
*      created: 2024-09-15 15:37:36
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const int INF = 0x3f3f3f3f;
const int N = 1e9 + 7;
LL n,k;


LL check(LL i)
{
    return (-2*k*n+4*k*i-n*n+n+2*i*i-2*i) / 2;
}

void solve()
{
    cin>>n>>k;
    int l = 1,r = n;
    while(l < r)
    {
        LL mid = (r + l) >> 1;
        if(check(mid) < 0) l = mid + 1;
        else r = mid;
    }
    cout<<min(abs(check(l)),abs(check(l-1)))<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}